<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/4/10
 * Time: 14:20
 */

namespace Biz\service;

use Biz\model\activity\LimitTimeRobbing as mLimitTimeRobbing;
use Biz\model\activity\LimitTimeRobbingSku;
use Biz\model\ShopCarouselBanner;
use Biz\module\Packages\Recommend\RecommendGoods;
use Biz\module\Packages\Special\LargeSpecial;
use Biz\module\Packages\Special\SmallSpecial;
use Doraemon\tools\Log as tLog;
use Biz\cache\ShopRecommendSpecial as cShopRecommendSpecial;
use Biz\service\activity\FlashSale as sFlashSale;
use Biz\service\MallGoodsList as sMallGoodsList;
/**
 * @deprecated
 */
class ShopMall
{
    const SPECIAL_CHCHE_NAME  = 'special';

    /*
     *  首页banner和分类入口
     * **/
    public static function getBanner($uid, $version)
    {

        try {

            $time = date('Y-m-d H:i:s');
            $data['slide']     = CarouselBanner::getBanner($uid,$time); // 获取banner

            if($version) {
                $data['category']  = ShopCategoryEntrance::getCategory_347($time); // 获取分类入口信息
            } else {
                $data['category']  = ShopCategoryEntrance::getCategory($time); // 获取分类入口信息
            }
            $data['list'] = CarouselBanner::getMultipleBanner($uid, $time);

            return $data;
        } catch (\Exception $e) {

            tLog::warning('用户获取商城banner信息失败', [
                'uid'       => $uid,
                'edittime' => date('Y-m-d H:i:s'),
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);

            throw $e;
        }
    }

    /*
     *  首页推荐
     * **/
    public static function getRecommendInfo($uid, $page, $areaCode, $version)
    {

        try {
            /*
             * 获取大小专题
             * **/
            {
                $cacheSpecialData = cShopRecommendSpecial::getSpecial(self::SPECIAL_CHCHE_NAME);
                if (is_null($cacheSpecialData)){
                    $specialTime      = date('Y-m-d H:i:s');
                    $largeSpecialInfo = (new LargeSpecial())->getSpecial($specialTime); // 获取大专题
                    $smallSpecialInfo = (new SmallSpecial())->getSpecial($specialTime); // 获取小专题
                    $cacheSpecialData      = array_merge($largeSpecialInfo, $smallSpecialInfo);
                    cShopRecommendSpecial::setSpecial(self::SPECIAL_CHCHE_NAME, $cacheSpecialData);
                }

                $smallSpecialOrBannerInfo = [];
                $largeSpecialInfo = (new LargeSpecial())->formatSpecial($cacheSpecialData, $is_large_special = true , $page);
                if (version_compare($version, "3.9.0", ">=")) {
                    $bannerInfos = CarouselBanner::getMultipleBanner($uid, date("Y-m-d H:i:s"));
                    $bannerInfos = array_column($bannerInfos, null, 'type');
                    $bannerInfos = $bannerInfos[ShopCarouselBanner::TYPE_SUMITOMO_IS_BUYING]['mall_sumitomo_is_buying'];
                    if($bannerInfos) {
                        $bannerInfos = array_slice($bannerInfos, ($page - 1) * 2, 2);
                        foreach ($bannerInfos as $k => $bannerInfo) {
                            $smallSpecialOrBannerInfo[] = [
                                'type' => ShopCarouselBanner::TYPE_APP_BANNER_INFO,
                                'banner_info' => array_merge( $bannerInfo, ['index' => ($page-1)*2+$k+1]) //统计需求
                            ];
                        }
                    }
                } else {
                    $smallSpecialOrBannerInfo = (new SmallSpecial())->formatSpecial($cacheSpecialData, $is_large_special = false, $page);
                }
                $pageSize         = LargeSpecial::getPageSize($largeSpecialInfo, $smallSpecialOrBannerInfo); // 每页所需数据量
            }

            /*
             * 获取商品信息
             * **/
            {
                $goodsInfo = (new RecommendGoods())->getRecommendGoodsInfo($uid, $areaCode, $page, $largeSpecialInfo, $smallSpecialOrBannerInfo, $pageSize);
            }

            /*
             * 返回数据
             * **/
            {
                $is_over = 0;
                if ($goodsInfo['count'] != 20 || $page == 15) {
                    $is_over = 1;
                }

                $data = array(
                    'rows' => ($goodsInfo['goods'] && is_array($goodsInfo['goods'])) ? $goodsInfo['goods'] : array(),
                    'is_over' => $is_over,
                );
            }

            return $data;
        } catch (\Exception $e) {

            tLog::warning('用户获取商城首页数据失败', [
                'uid' => $uid,
                'edittime' => date('Y-m-d H:i:s'),
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);

            throw $e;
        }
    }


    public static function LimitRobPreview()
    {
        $preList = Activitys::LimitTimeRobHappening();
        $rst = [
            'time_down' => null,
            'goods_list' => [],
            'is_has_trailer' => 0
        ];


        if ($preList['goods_list']) {

            $goodsIds = array_column($preList['goods_list'], 'id');
            $priceRange = self::InLimitTimeSkuPrice($goodsIds);
            $rst['time_down'] = $preList['time_down'];
            $rst['goods_list'] = array_slice($preList['goods_list'], 0, 3);
            $rst['is_sell_out'] = 1;
            foreach ($rst['goods_list'] as $goodsinfo) {
                if ($goodsinfo['active_top']['params']['stock']) {
                    $rst['is_sell_out'] = 0;
                    break;
                }
            }

            $rst['is_has_trailer'] = mLimitTimeRobbing::TraitList() ? 1 : 0;
            foreach ($rst['goods_list'] as &$goodsInfo) {
                $goodsInfo['min_price'] = $goodsInfo['max_price'] = $priceRange[$goodsInfo['id']][0];
            }

        }


        return $rst;

    }

    /*
     * 商城tab限时购
     * **/
    public static function flashSaleIng($isPreview = true)
    {
        try {

            $flashSaleData = sFlashSale::flashSaleIng($isPreview);

            return $flashSaleData;

        } catch (\Exception $e) {

            tLog::warning('用户获取商城[进行中]限时抢信息失败', [
                'edittime' => date('Y-m-d H:i:s'),
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);

            throw $e;
        }

    }

    public static function InLimitTimeSkuPrice($goodsIds, $time = '')
    {
        if (!$time) {
            $time = date('Y-m-d H:i:s');
        }
        $infos = LimitTimeRobbingSku::getInfosByGid($goodsIds, $time);
        $Rst = [];
        foreach ($infos as $goodsId => $info) {
            $skuIds = array_column($info, 'sku_id');
            $skuInfos = SkuInfo::getSkuList([], $skuIds, false);
            $pricelist = array_column($skuInfos, 'price');
            $Rst[$goodsId] = [min($pricelist), max($pricelist)];
        }
        return $Rst;
    }

    public static function multipleBanner($uid, $version, $areaCode)
    {
        try {

            $time = date('Y-m-d H:i:s');
            $data['slide']     = CarouselBanner::getBanner($uid, $time); // 获取banner
            $list = CarouselBanner::getMultipleBanner($uid, $time, $areaCode); // 活动位置
            $data['list'] = [];
            $data['category'] = [];
            if(version_compare($version, "3.9.0", ">=")) {
                $hayStack = [ShopCarouselBanner::TYPE_ONE, ShopCarouselBanner::TYPE_THREE, ShopCarouselBanner::TYPE_FOUR, ShopCarouselBanner::TYPE_TWO_ONELINE];
                if (version_compare($version, '3.12.0', ">=")) {
                    $hayStack[] = ShopCarouselBanner::TYPE_FLASH_MODULE;
                }
            	if (version_compare($version, '3.16', ">=")) {
                    $hayStack = array_merge($hayStack, [ShopCarouselBanner::TYPE_PINTUAN]);
				}
            	if(version_compare($version, '3.20', "<")) {
                    $hayStack = array_merge($hayStack, [ShopCarouselBanner::TYPE_GOODS_LIST, ShopCarouselBanner::TYPE_THREE_TITLE]);

                }

                foreach ($list as $item) {
                    if (  in_array($item['type'], $hayStack)) {
                        $data['list'][] = $item;
                    } else if ($item['type'] == ShopCarouselBanner::TYPE_FIVE_POSITION) {
                        $data['category'] = $item['mall_five_position_list'];
                    }
                }
            } else {
                $data['category']  = ShopCategoryEntrance::getCategory_347($time); // 获取分类入口信息
                foreach ($list as $item) {
                    if (  in_array($item['type'], [ShopCarouselBanner::TYPE_ONE, ShopCarouselBanner::TYPE_THREE, ShopCarouselBanner::TYPE_FOUR])) {
                        $data['list'][] = $item;
                    }
                }
            }
            return $data;
        } catch (\Exception $e) {

            tLog::warning('用户获取商城banner活动信息失败', [
                'uid'       => $uid,
                'edittime' => date('Y-m-d H:i:s'),
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);

            throw $e;
        }
    }

    public static function RecommendByTag($uid, $params, $page, $pageSize, $sortType, $areaCode)
    {

        try{

            $params['params'] = isset($params['params']) ? $params['params'] : [];

            return sMallGoodsList::RecommendByTag($params['tag'], $page, $pageSize, $sortType, $areaCode, $params['params']);

        }  catch (\Exception $e) {

            tLog::warning('用户根据标签获取商品失败', [
                'uid'       => $uid,
                'tag'       => $params['tag'],
                'edittime' => date('Y-m-d H:i:s'),
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);

            throw $e;
        }
    }

    public static function RecommendByKeyWord($uid, $keyWord, $page, $pageSize, $areaCode)
    {
        try{

            return sMallGoodsList::RecommendByKeyWord($keyWord, $page, $pageSize, $areaCode);

        }  catch (\Exception $e) {

            tLog::warning('用户根据关键字获取商品失败', [
                'uid'       => $uid,
                'keyword'       => $keyWord,
                'edittime' => date('Y-m-d H:i:s'),
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);

            throw $e;
        }
    }

}